package com.exartisan.network;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.exartisan.bean.Category;
import com.exartisan.bean.Event;
import com.exartisan.bean.ForumCategory;
import com.exartisan.bean.GalleryContent;
import com.exartisan.bean.Post;
import com.exartisan.bean.Quiz;
import com.exartisan.bean.Survey;

public class ResponseTranslater {

	 public static boolean checkSuccess(String jsonResponse)
	 {
	 JSONObject jsonObject;
	 boolean isSuccess = false;
	 try
	 {
	 jsonObject = new JSONObject(jsonResponse);
	 if (jsonObject.getString(NetworkUtility.HANDLE).equals(
	 NetworkUtility.ON_SYSTEM_ERROR)) isSuccess = false;
	 else isSuccess = true;
	 }
	 catch (JSONException e)
	 {
	 // TODO: handle exception
	 }
	 return isSuccess;
	 }
	
	 public static String getHandle(String jsonResponse)
	 {
	 JSONObject jsonObject;
	 String handle = "";
	 try
	 {
	 jsonObject = new JSONObject(jsonResponse);
	 handle = jsonObject.getString(NetworkUtility.HANDLE);
	 }
	 catch (JSONException e)
	 {
	 // TODO Auto-generated catch block
	 e.printStackTrace();
	 }
	 return handle;
	 }
	
	 public static String getErrorMessage(String jsonResponse)
	 {
	 JSONObject jsonObject;
	 String msg = "";
	 try
	 {
	 jsonObject = new JSONObject(jsonResponse);
	 msg = jsonObject.getString(NetworkUtility.ERROR_MESSAGE);
	 }
	 catch (JSONException e)
	 {
	
	 }
	 return msg;
	 }

	// public static ArrayList<Event> getTopEvent(String jsonResponse)
	// {
	// ArrayList<Event> alEvents = new ArrayList<Event>();
	// long eventID;
	// int type; // 0: top event | 1: new event
	// String eventName;
	// String startDate;
	// String endDate;
	// String thumbUrl;
	// String fbFeedID = null;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// eventID = jsonElement.getLong(NetworkUtility.ID);
	// type = 0;
	// eventName = jsonElement.getString(NetworkUtility.EVENTNAME);
	// startDate = jsonElement.getString(NetworkUtility.STARTDATE);
	// endDate = jsonElement.getString(NetworkUtility.ENDDATE);
	// thumbUrl = jsonElement.getString(NetworkUtility.THUMBURL);
	// Event event = new Event(eventID, type, eventName, startDate,
	// endDate, thumbUrl, fbFeedID);
	// event.setArticle(jsonElement.getString(NetworkUtility.ARTICLE));
	// event.setDescription(jsonElement
	// .getString(NetworkUtility.DESCRIPTION));
	// event.setCoverUrl(jsonElement
	// .getString(NetworkUtility.THUMBURL));
	// event.setFbFeedID(jsonElement.getString(NetworkUtility.FEEDID));
	// alEvents.add(event);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return alEvents;
	// }

	// public static ArrayList<Event> getNewEvent(String jsonResponse)
	// {
	// ArrayList<Event> alEvents = new ArrayList<Event>();
	// long eventID;
	// int type; // 0: top event | 1: new event
	// String eventName;
	// String startDate;
	// String endDate;
	// String thumbUrl;
	// String description;
	// String fbFeedID = null;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// eventID = jsonElement.getLong(NetworkUtility.ID);
	// type = 0;
	// eventName = jsonElement.getString(NetworkUtility.EVENTNAME);
	// startDate = jsonElement.getString(NetworkUtility.STARTDATE);
	// endDate = jsonElement.getString(NetworkUtility.ENDDATE);
	// thumbUrl = jsonElement.getString(NetworkUtility.THUMBURL);
	// description = jsonElement.getString(NetworkUtility.DESCRIPTION);
	// Event event = new Event(eventID, type, eventName, startDate,
	// endDate, thumbUrl, description, fbFeedID);
	// event.setArticle(jsonElement.getString(NetworkUtility.ARTICLE));
	// event.setCoverUrl(jsonElement
	// .getString(NetworkUtility.THUMBURL));
	// event.setFbFeedID(jsonElement.getString(NetworkUtility.FEEDID));
	// alEvents.add(event);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return alEvents;
	// }

	// public static ArrayList<Gallery> getGalleryInfo(String jsonResponse)
	// {
	// ArrayList<Gallery> galleries = new ArrayList<Gallery>();
	// long galleryID;
	// long eventID;
	// String eventName;
	// String thumbURL;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// galleryID = jsonElement.getLong(NetworkUtility.ID);
	// eventID = jsonElement.getLong(NetworkUtility.EVENTID);
	// eventName = jsonElement.getString(NetworkUtility.EVENTNAME2);
	// thumbURL = jsonElement.getString(NetworkUtility.MEDIA_URL);
	// Gallery gallery = new Gallery(galleryID, eventID, eventName,
	// thumbURL);
	// galleries.add(gallery);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return galleries;
	// }
	//
	// public static ArrayList<GalleryContent> getEventGallery(String
	// jsonResponse)
	// {
	// ArrayList<GalleryContent> contents = new ArrayList<GalleryContent>();
	// long contentID;
	// String resourceUrl;
	// String description;
	// int type;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// contentID = jsonElement.getLong(NetworkUtility.ID);
	// resourceUrl = jsonElement.getString(NetworkUtility.MEDIA_URL);
	// type = jsonElement.getInt(NetworkUtility.TYPE);
	// description = jsonElement.getString(NetworkUtility.MEDIA_TITLE);
	// GalleryContent content = new GalleryContent(contentID,
	// resourceUrl, description, type);
	// contents.add(content);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return contents;
	// }

	// public static Event getDetailEvent(String jsonResponse)
	// {
	// Event event = new Event();
	// String startDate;
	// String endDate;
	// String description;
	// String article;
	// String place;
	// String coverUrl;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NetworkUtility.QUESTION);
	// JSONObject jsonTable = jsonQuestion
	// .getJSONObject(NetworkUtility.TABLE);
	// event.setEventID(jsonTable.getLong(NetworkUtility.ID));
	// event.setEventName(jsonTable.getString(NetworkUtility.EVENTNAME));
	// event.setStartDate(jsonTable.getString(NetworkUtility.STARTDATE));
	// event.setCoverUrl(jsonTable.getString(NetworkUtility.THUMBURL));
	// event.setEndDate(jsonTable.getString(NetworkUtility.ENDDATE));
	// event.setPlace(jsonTable.getString(NetworkUtility.PLACE));
	// event.setArticle(jsonTable.getString(NetworkUtility.ARTICLE));
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return event;
	// }

	/**
	 * Parser data envent
	 * 
	 * @param jsonEvent
	 * @return
	 */
	public static ArrayList<Event> getEvent(String jsonEvent) {
		ArrayList<Event> alEvent = new ArrayList<Event>();
		int EventID;
		String EventName;
		String  FeedID;
		String DateCreate;
		String DateStart;
		String DateEnd;
		String Image;
		String ImageThumb;
		String EventContent;
		String EventAddress;
		try {
			JSONObject event_JsonObject = new JSONObject(jsonEvent);
			JSONObject jsonQuestion = event_JsonObject
					.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				EventID = jsonElement.getInt(NetworkUtility.EVENTID);
				EventName = jsonElement.getString(NetworkUtility.EVENTNAME2);
				FeedID = jsonElement.getString(NetworkUtility.FEEDID);
				DateCreate = jsonElement.getString(NetworkUtility.DATE_CREATE);
				DateStart = jsonElement.getString(NetworkUtility.DATE_START);
				DateEnd = jsonElement.getString(NetworkUtility.DATE_END);
				Image = jsonElement.getString(NetworkUtility.IMGS);
				ImageThumb = jsonElement.getString(NetworkUtility.IMG_THUMB);
				EventContent = jsonElement
						.getString(NetworkUtility.EVENT_CONTENT);
				EventAddress = jsonElement
						.getString(NetworkUtility.EVENT_ADDRESS);
				Event event = new Event(EventID, EventName, DateStart, DateEnd, ImageThumb, FeedID, EventAddress, EventContent, Image);
				event.setDescription(jsonElement.getString("EvenDes"));
				alEvent.add(event);
				//
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alEvent;
	}
	/**
	 * parser data posts
	 * 
	 * @param jsonPost
	 * @return
	 */
	public static ArrayList<Post> getPosts(String jsonPost) {
		ArrayList<Post> alPost = new ArrayList<Post>();
		int PostID;
		int CategoryID;
		int PostType;
		String PostDescription;
		String PostContent;
		String DateCreate;
		int UserCreate;
		String Image;
		String ImageThumb;
		String FeedID;
		try {
			JSONObject event_JsonObject = new JSONObject(jsonPost);
			JSONObject jsonQuestion = event_JsonObject
					.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				PostID = jsonElement.getInt(NetworkUtility.POST_ID);
				CategoryID = jsonElement.getInt(NetworkUtility.CATEGORY_ID);
				PostType = jsonElement.getInt(NetworkUtility.POST_TYPE);
				PostDescription = jsonElement
						.getString(NetworkUtility.POST_DESCRIPTION);
				PostContent = jsonElement
						.getString(NetworkUtility.POST_CONTENT);
				DateCreate = jsonElement.getString(NetworkUtility.DATE_CREATE);
				UserCreate = jsonElement.getInt(NetworkUtility.USER_CREATE);
				Image = jsonElement.getString(NetworkUtility.IMG);
				ImageThumb = jsonElement.getString(NetworkUtility.IMG_THUMB);
				FeedID = jsonElement.getString(NetworkUtility.FEEDID);
				Post post = new Post(PostID, CategoryID, PostType, PostDescription, PostContent, DateCreate, UserCreate+"", FeedID, Image, ImageThumb);
				post.setTitle(jsonElement.getString(NetworkUtility.POST_TITLE));
				post.setUserName(jsonElement.getString(NetworkUtility.POST_USER_NAME));
				post.setUserAvatarUrl(jsonElement.getString(NetworkUtility.POST_USER_AVATAR_URL));
				alPost.add(post);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alPost;
	}

	/**
	 * parser data gallery
	 * 
	 * @param jsonGallery
	 * @return
	 */
	public static ArrayList<GalleryContent> getDetailGallery(String jsonGallery) {
		ArrayList<GalleryContent> alContent = new ArrayList<GalleryContent>();
		int MediaID;
		int ParentType;
		int ParentID;
		String MediaTitle;
		String MediaDescription;
		int MediaType;
		String MediaLink;
		String Image;
		String ImageThumb;
		try {
			JSONObject gallerryJsonObject = new JSONObject(jsonGallery);
			JSONObject jsonQuestion = gallerryJsonObject
					.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				MediaID = jsonElement.getInt(NetworkUtility.MEDIA_ID);
				ParentType = jsonElement.getInt(NetworkUtility.PARENT_TYPE);
				ParentID = jsonElement.getInt(NetworkUtility.PARENT_ID);
				MediaTitle = jsonElement.getString(NetworkUtility.MEDIA_TITLE);
				MediaDescription = jsonElement
						.getString(NetworkUtility.MEDIA_DESCRIPTION);
				MediaType = jsonElement.getInt(NetworkUtility.MEDIA_TYPE);
				MediaLink = jsonElement.getString(NetworkUtility.MEDIA_LINK);
				Image = jsonElement.getString(NetworkUtility.IMG);
				ImageThumb = jsonElement.getString(NetworkUtility.IMG_THUMB);
				GalleryContent content = new GalleryContent(MediaID, Image, MediaDescription, MediaTitle, MediaType);
				alContent.add(content);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alContent;
	}

	/**
	 * parser data 4rum's category
	 * 
	 * @param json4rum
	 * @return
	 */
	public static ArrayList<ForumCategory> getCategoryForum(String json4rum) {
		ArrayList<ForumCategory> categories = new ArrayList<ForumCategory>();
		int CatID;
		String CatTitle;
		String DateCreate;
		int UserCreate;

		try {
			JSONObject cate4rum_JsonObject = new JSONObject(json4rum);
			JSONObject jsonQuestion = cate4rum_JsonObject
					.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElementJ = jsonTable.getJSONObject(i);
				//
				CatID = jsonElementJ.getInt(NetworkUtility.CAT_ID);
				CatTitle = jsonElementJ.getString(NetworkUtility.CAT_TITLE);
				DateCreate = jsonElementJ.getString(NetworkUtility.DATE_CREATE);
				UserCreate = jsonElementJ.getInt(NetworkUtility.USER_CREATE);
				ForumCategory category = new ForumCategory();
				category.setTitle(CatTitle);
				category.setCategoryId(Long.valueOf(CatID));
				categories.add(category);
				//
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return categories;
	}

	/**
	 * parser data Servey
	 * 
	 * @param jsonSurvey
	 * @return
	 */
	public static ArrayList<Survey> getSurvey(String jsonSurvey) {
		ArrayList<Survey> alSurvey = new ArrayList<Survey>();
		int SurveyID;
		String SurveyName;
		int EventID;
		String DateCreate;
		String DateStart;
		String DateEnd;
		String Image;
		String ImageThumb;
		int status;

		try {
			JSONObject servey_JsonObject = new JSONObject(jsonSurvey);
			JSONObject jsonQuestion = servey_JsonObject
					.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonArrayTable = jsonQuestion
					.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonArrayTable.length(); i++) {
				JSONObject elementJsonObject = jsonArrayTable.getJSONObject(i);
				//
				SurveyID = elementJsonObject.getInt(NetworkUtility.SURVEY_ID);
				SurveyName = elementJsonObject
						.getString(NetworkUtility.SURVEY_NAME);
				EventID = elementJsonObject.getInt(NetworkUtility.EVENTID);
				DateCreate = elementJsonObject
						.getString(NetworkUtility.DATE_CREATE);
				DateStart = elementJsonObject
						.getString(NetworkUtility.DATE_START);
				DateEnd = elementJsonObject.getString(NetworkUtility.DATE_END);
				Image = elementJsonObject.getString(NetworkUtility.IMG);
				ImageThumb = elementJsonObject
						.getString(NetworkUtility.IMG_THUMB);
				status = elementJsonObject.getInt(NetworkUtility.STATUS);
				Survey survey = new Survey(SurveyID, SurveyName, DateCreate, DateStart, DateEnd, status);
				survey.setThumbUrl(ImageThumb);
				alSurvey.add(survey);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alSurvey;
	}

	/**
	 * parser data Questions
	 * 
	 * @param jsonSurvey
	 * @return
	 */
	public static ArrayList<Quiz> getQuestion(String jsonQuestions) {
		ArrayList<Quiz> alQuiz = new ArrayList<Quiz>();
		int QuestionID;
		String QuestionName;
		int QuestionType;
		int SurveyID;

		try {
			JSONObject question_JsonObject = new JSONObject(jsonQuestions);
			JSONObject jsonQuestion = question_JsonObject
					.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonArrayTable = jsonQuestion
					.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonArrayTable.length(); i++) {
				JSONObject elementJsonObject = jsonArrayTable.getJSONObject(i);
				//
				QuestionID = elementJsonObject
						.getInt(NetworkUtility.QUESTION_ID);
				QuestionName = elementJsonObject
						.getString(NetworkUtility.QUESTION_NAME);
				QuestionType = elementJsonObject
						.getInt(NetworkUtility.QUESTION_TYPE);
				SurveyID = elementJsonObject.getInt(NetworkUtility.SURVEY_ID);
				Quiz quiz = new Quiz(QuestionID, QuestionName, 1000, null);
				alQuiz.add(quiz);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alQuiz;
	}
}